<?php
/* 


/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
	Group_subject	:	Class Group_subject    
\												/

*/
	class Group_subject
	{
		private $id;
		private $dId;
		private $number;
		private $name;
		
		function __construct()
		{
			
		}
		
		function getID()
		{
			return $this->id;
		}
		
		function setID($set)
		{
			$this->id = $set;
		}
		
		function getDID()
		{
			return $this->dId;
		}
		
		function setDID($set)
		{
			$this->dId = $set;
		}
		
		function getNember()
		{
			return $this->number;
		}
		
		function setNumber($set)
		{
			$this->number = $set;
		}
		
		function getName()
		{
			return $this->name;
		}
		
		function setName($set)
		{
			$this->name = $set;
		}
		
		function setAll($id,$dId,$number,$name)
		{
			$this->id = $id;
			$this->dId = $dId;
			$this->number = $number;
			$this->name = $name;
		}
		
		function findOne()
		{
			$query = mysql_query('SELECT G_name,G_D_id,G_name,G_number FROM subject_group WHERE G_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			$this->dId = $arr->G_D_id;
			$this->number = $arr->G_number;
			$this->name = $arr->G_name;
			return($this->name);
		}
		
		function findDepId()
		{
			$query = mysql_query('SELECT G_D_id FROM subject_group WHERE G_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			return($arr->G_D_id);
		}
		
		/*function findWhere()
		{
			$query = mysql_query('SELECT G_id,G_name,G_D_id FROM subject_group WHERE G_D_id = "'.$this->did.'"')or die(mysql_error());
			while($arr = mysql_fetch_object($query))
			{
				$data_array[$conut]['id'] = $arr->G_id;
				$data_array[$conut]['name'] = $arr->G_name;
				$data_array[$conut]['number'] = $arr->G_number;
				$conut++;
			}
			return json_encode($data_array);
		}*/
		
		function findAll()
		{
			include('deparment.php');
			$dep = new Deparment;
			
			$query = mysql_query('SELECT G_id,G_D_id,G_name,G_number FROM subject_group ORDER BY `G_D_id`, `G_number`')or die(mysql_error());
			$data_array = array();
			$conut = 1;
			while($arr = mysql_fetch_object($query))
			{
				$data_array[$conut]['id'] = $arr->G_id;
				$data_array[$conut]['did'] = $arr->G_D_id;
				$data_array[$conut]['name'] = $arr->G_name;
				$data_array[$conut]['number'] = $arr->G_number;
				$dep->setID($arr->G_D_id);
				$data_array[$conut]['did'] =$dep->getID();
				$data_array[$conut]['dName'] =$dep->findOne();
				$conut++;
			}
			return json_encode($data_array);
		}
		
		function add()
		{
			mysql_query('INSERT INTO subject_group(G_D_id,G_name,G_number) VALUES("'.$this->dId.'","'.$this->name.'","'.$this->number.'")')or die(mysql_error());
		}
		
		function edit()
		{
			mysql_query('UPDATE subject_group SET G_D_id="'.$this->dId.'",G_name="'.$this->name.'",G_number="'.$this->number.'" WHERE G_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
		}
		
		function del()
		{
			mysql_query('DELETE FROM subject_group WHERE G_id="'.$this->id.'" LIMIT 1') or die(mysql_error());
		}
		
	}
?>